﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 大话设计模式
{
    class Program
    {
        static void Main(string[] args)
        {
            //时间：2014-2-25 23:24:59
            //原因：C#高级编程数组有个概念涉及到了 迭代器 的模式
            //目标：参考本书的 迭代器 模式，测试相关代码，理解内部机制
            Sample_01();
            Sample_02();
            Sample_03();
            Console.ReadKey();
        }
        //官方实例
        static void Sample_01()
        {
            Console.WriteLine("--------------示例1---------------");
            ConcreteAggregate a = new ConcreteAggregate();
            a[0] = "大鸟";
            a[1] = "小菜";
            a[2] = "行李";
            a[3] = "老外";
            a[4] = "公交内部员工";
            a[5] = "小偷";
            Iterator i = new ConcreteIterator(a);
            //object item = i.First();//不重要
            while (!i.IsDone())//true是继续执行
            {
                Console.WriteLine("{0} 请买车票！",i.CurrentItem());
                i.Next();
            }
 
        }
        static void Sample_02()
        {
            Console.WriteLine("--------------示例2---------------");
            Person p = new Person();
            p.Add(new Person { Id = 1, Name = "Li" });
            p.Add(new Person { Id = 12, Name = "Hi" });
            p.Add(new Person { Id = 3, Name = "Lei" });
            p.Add(new Person { Id = 4, Name = "Jim" });
            foreach (Person item in p)
            {
                Console.WriteLine("Person Id：{0}，Name：{1}",item.Id,item.Name);
            }
        }
        static void Sample_03()
        {
            Console.WriteLine("--------------示例3---------------");
            PersonAggregate[] pa = new PersonAggregate[] 
            {
                new PersonAggregate{Id = 1, Name = "Li"},
                new PersonAggregate{Id = 12, Name = "Hi"},
                new PersonAggregate{Id = 3, Name = "Lei"},
                new PersonAggregate{Id = 1, Name = "Kim"},
            };
            PersonIterator ii = new PersonIterator(pa);
            while (ii.IsDone())
            {
                Console.WriteLine("Person Id：{0}，Name：{1}", ((PersonAggregate)ii.CurrentItem()).Id, ((PersonAggregate)ii.CurrentItem()).Name);
                ii.Next();
            }
           
        }
    }
}
